package jp.co.nes.awssupport.common.utility;

import java.util.ArrayList;
import java.util.List;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.DescribeReservedInstancesRequest;
import com.amazonaws.services.ec2.model.DescribeReservedInstancesResult;
import com.amazonaws.services.ec2.model.ReservedInstances;

import jp.co.nes.awssupport.common.base.CommonException;

/**
 * EC2ヘルパーです。<br>
 * EC2に関する機能を提供します。
 */
public class AwsEc2Helper extends AwsHelperBase {

    /** クライアント */
    private AmazonEC2 client;

    /**
     * デフォルトコンストラクタです。
     */
    public AwsEc2Helper() {
    }

    /**
     * リージョンを引数にもつコンストラクタです。
     *
     * @param regions リージョン
     * @param accessKey アクセスキー
     * @param secretKey シークレットキー
     */
    public AwsEc2Helper(Regions regions, String accessKey, String secretKey) {
        super(regions, accessKey, secretKey);
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.utility.AwsHelperBase#init(com.amazonaws.regions.Regions, com.amazonaws.auth.AWSCredentials, com.amazonaws.ClientConfiguration)
     */
    @Override
    protected void init(Regions regions, AWSCredentials credentials, ClientConfiguration config) {
        boolean isLocalTest = Boolean.valueOf(PropertiesUtil.getProperty("LocalTest", "false"));
        if (!isLocalTest) {
            if (credentials == null) {
                if (config == null) {
                    client = AmazonEC2ClientBuilder.standard()
                            .withRegion(regions)
                            .build();
                } else {
                    client = AmazonEC2ClientBuilder.standard()
                            .withRegion(regions)
                            .withClientConfiguration(config)
                            .build();
                }
            } else {
                if (config == null) {
                    client = AmazonEC2ClientBuilder.standard()
                            .withRegion(regions)
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(new ClientConfiguration())
                            .build();
                } else {
                    client = AmazonEC2ClientBuilder.standard()
                            .withRegion(regions)
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(config)
                            .build();
                }
            }
        }
    }

    /**
     * ReservedInstancesを返却します。<br>
     * reservedInstancesIdに対応するReservedInstancesがなかった場合はnullを返却します。
     *
     * @param reservedInstancesId reservedInstancesId
     * @return ReservedInstances
     */
    public ReservedInstances getReservedInstances(String reservedInstancesId) {
        DescribeReservedInstancesRequest request = new DescribeReservedInstancesRequest();
        request.setReservedInstancesIds(new ArrayList<String>() {
            {
                add(reservedInstancesId);
            }
        });
        DescribeReservedInstancesResult result = client.describeReservedInstances(request);

        ReservedInstances reservedInstances = null;
        if (result != null && result.getReservedInstances() != null) {
            List<ReservedInstances> reservedInstancesList = result.getReservedInstances();
            int size = reservedInstancesList.size();
            if (size == 1) {
                reservedInstances = reservedInstancesList.get(0);
            } else if (size > 1) {
                throw new CommonException("size > 1(size=" + size + ")");
            }
        }
        return reservedInstances;
    }
}
